window.onload = function() {

    layui.use(['jquery', 'layer'], function() {
        var $ = layui.jquery,
            layer = layui.layer;


        var flag2 = true;
        /**
         * 提现服务/手续费
         * @param $this
         */
        function xzasdasd($this, service_rate, service_money) {
            var money = $($this).val();
            // var flag=true;
            if (money != '' && money >= 0) {
                var html = '<p class="boxtop" style="margin-bottom:10px; font-size:15px;"><span class="txtitle">手续费</span><span class="rmb sxf">￥</span></p>' +
                    '<p class="txtexts"  style="margin-bottom:10px; font-size:15px;">微信平台转账手续费率 0.1%</p>' +
                    '<p class="txtexts" style="margin-bottom:10px; font-size:15px;">平台服务费率 ' + service_rate + '%</p>' +
                    '<p class="boxtop"  style="margin-bottom:10px; font-size:15px;"><span class="txtitle">交易服务费</span><span class="rmb fwf">￥' + service_money + '(自动扣除)</span></p>';
                if (flag2) {
                    $('.flag1').after(html);
                    flag2 = false;
                    var Height = 0;
                    $("#6>div>p").each(function(i) {
                        Height += $("#6>div>p").eq(i).height();
                    })
                    $("#6").height(Height + 80)
                }

                var sxf = 1;
                if (money > 1000) {
                    sxf = money * 0.001;
                } else if (money >= 25000) {
                    sxf = 25;
                }
                service_rate = service_rate / 100;
                sxf = sxf + money * service_rate;

                fl = Math.round(sxf * 100) / 100;

                $('.sxf').html(fl.toFixed(2));
            } else if (money == '') {
                var len = $("#6>div>p").length;
                if (len > 3) {
                    for (var i = 0; i < 3; i++) {
                        $("#6>div>p:last-child").remove()
                    }
                    $("#6").height(100);
                    flag2 = true;
                }
            } else if (money < 0) {
                Utils.layer.msg("请输入正确的金额")
            } else if (typeof money != 'number') {
                Utils.layer.msg("请输入数字")
            }

        }
        $('body').on("click", ".layui-layer-close1", function() {
            flag2 = true;
        })

        function getshopmoney() {
            var shopmoneys;
            var money;
            var service_money;
            var service_rate;
            $.ajax({
                'type': 'POST',
                'url': '/withdraw/getshop',
                'async': false,
                'dataType': 'json',
                'success': function(data) {
                    shopmoneys = data.data.order_money
                    money = data.data.sum
                    service_money = data.data.service_money
                    service_rate = data.data.service_rate
                }
            })
            return [shopmoneys, money, service_money, service_rate];
        }

        function getchecknum(phone) {
            getCheckNum(phone, ".getchecknum");
        }

        function getchecknums(phone) {
            getCheckNum(phone, ".getchecknums");
        }

        function getchecknumse(phone) {
            getCheckNum(phone, ".getchecknumse");
        }
        // 获取验证码
        function getCheckNum(phoneNum, CheckNumBtn) {
            var phone_num = phoneNum;
            if (/^[1][3,4,5,7,8][0-9]{9}$/.test(phone_num)) {
                var second = 90;
                var timer = setInterval(function() {
                    second--;
                    if (second != 0) {
                        $(CheckNumBtn).css({ "background": "#999", "color": "#eee" }).val("重新发送" + second + "s");
                        $(CheckNumBtn).attr("disabled", "disabled");
                    } else {
                        $(CheckNumBtn).val("获取验证码").css({ "background": "#08c", "color": "#fff" });
                        $(CheckNumBtn).removeAttr("disabled");
                        clearInterval(timer);
                    }
                }, 1000);
                $.post("/code/send-code", { phone: phone_num }, function success(data) {
                    if (data.code == 200) {
                        Utils.layer.msg(data.msg);
                    } else { Utils.layer.msg(data.msg); }
                });
            } else {
                Utils.layer.msg("您输入的电话有误，请重新输入")
            }
        }
        $(function() {


            var shopdata;
            $.ajax({
                'type': 'POST',
                'url': '/withdraw/getshop',
                'async': false,
                'dataType': 'json',
                'success': function(data) {
                    shopdata = data.data;
                }
            });

            $('.ispwd').click(function() {
                var isbtn = $(this).attr('attrs');
                var pwd = $('.hidpwd').val();
                //验证是否输入密码
                if (pwd) {
                    if (isbtn == 'btnwx') {
                        Utils.layer.msg('提现到微信暂未开放');
                        return;
                        var openid = shopdata.openid;
                        if (!openid) {
                            var codeurl = $('.ewmurl').val();
                            Utils.layer.open({
                                id: 5,
                                type: 1,
                                title: '请扫描小程序二维码绑定商户信息',
                                skin: 'layui-layer-rim',
                                area: ['auto', '530px'],
                                content: '<img src="' + codeurl + '">',
                                btn: ['绑定完成后请刷新页面', '取消'],
                                btn1: function(index, layero) {
                                    window.location.reload()
                                }
                            })
                            return;
                        }
                        var shopmoneyss = getshopmoney();
                        Utils.layer.open({
                            id: 2,
                            type: 1,
                            title: '请输入提现金额',
                            skin: 'layui-layer-rim',
                            area: ['350px', 'auto'],
                            content: '<div style="margin: 20px auto 0; width:90%; color:#666; ">' +
                                '<p class="boxtop" style="margin-bottom:10px; font-size:15px;"><span class="txtitle">可提现余额</span><span class="rmb"> ¥' + shopmoneyss[0] + '</span></p>' +
                                '<p class="boxtop"  style="margin-bottom:10px; font-size:15px;"><span class="txtitle">提现金额</span> ¥<input type="test" class="txmoney layui-input" style="display:inline-block; width: 150px; margin-left:15px;" ></p>' +
                                '<p class="txtexts"  style="margin-bottom:10px; font-size:15px;"></p>' +
                                '</div>',
                            btn: ['提交提现申请', '取消'],
                            btn1: function(index, layero) {
                                var txmoneys = $('.txmoney').val();

                                if (parseFloat(txmoneys) > parseFloat(shopmoneyss)) {
                                    Utils.layer.msg('提现金额不能超过可提现余额');
                                    return;
                                } else {
                                    //TODO 微信提现
                                    $.ajax({
                                        'type': 'POST',
                                        'url': '/withdraw/add-wihdraw',
                                        'data': { withdraw_money: txmoneys, type: 0 },
                                        'dataType': 'json',
                                        'success': function(data) {
                                            if (data.code == 200) {
                                                var shopmoney;
                                                Utils.layer.msg('提现申请成功,待审核')
                                                Utils.layer.close(index);
                                                $('.yueval').html(data.data.order_money);
                                                $('.order_money').html(shopdata.money);
                                                $('.txcount').html(data.data.count);
                                            } else {
                                                Utils.layer.msg(data.msg)
                                            }
                                        }
                                    });
                                }
                            }
                        })
                    } else if (isbtn == 'btnyhk') {
                        var shopmoneyss = getshopmoney();
                        var moneysum = shopmoneyss[1];
                        var service_money = shopmoneyss[2];
                        var service_rate = shopmoneyss[3];
                        if (moneysum >= 50000) {
                            Utils.layer.msg('您已超过当天提现限额,每天限额提现5W');
                            return
                        }
                        Utils.layer.open({
                            id: 6,
                            type: 1,
                            title: '请输入提现金额',
                            skin: 'layui-layer-rim',
                            area: ['350px', 'auto'],
                            content: '<div style="margin: 20px auto 0; width:90%; color:#666;">' +
                                '<p class="boxtop"  style="margin-bottom:10px; font-size:15px;"><span class="txtitle">可提现余额</span><span class="rmb"></span> ¥' + shopmoneyss[0] + '</p>' +
                                '<p class="boxtop"  style="margin-bottom:10px; font-size:15px;"><span class="txtitle">提现金额</span> ¥<input type="text" class="txmoneys layui-input xzasdasdcls"   style="display:inline-block; width: 100px; margin-left:15px;" ></p>' +
                                '<p class="txtexts flag1"  style="margin-bottom:10px; font-size:15px;"></p>' +

                                // onkeyup="xzasdasd(this,' + service_rate + ',' + service_money + ')"
                                // '<p class="boxtop"><span class="txtitle">手续费</span><span class="rmb sxf">￥</span></p>' +
                                // '<p class="txtexts">微信平台转账手续费率 0.1%</br>平台服务费率 '+ service_rate +'%</p>' +
                                // '<p class="boxtop"><span class="txtitle">交易服务费</span><span class="rmb fwf">￥'+service_money+'(自动扣除)</span></p>' +
                                '</div>',
                            btn: ['下一步', '取消'],
                            btn1: function(index, layero) {
                                var txmoneys = $('.txmoneys').val();
                                if (txmoneys == '') {
                                    Utils.layer.msg('请输入提现金额');
                                    return;
                                } else if (txmoneys < 0) {
                                    Utils.layer.msg("请输入正确的金额");
                                    return;
                                } else if (!/\d(\.\d{0,2})?/.test(txmoneys)) {
                                    Utils.layer.msg("请输入数字");
                                    return;
                                } else {
                                    var txmoneyse = parseFloat(txmoneys);
                                }

                                if (parseFloat(txmoneyse) > parseFloat(shopmoneyss[0])) {
                                    Utils.layer.msg('提现金额不能超过可提现余额');
                                    return;
                                }

                                if ((parseFloat(moneysum) + parseFloat(txmoneyse)) >= 50000) {
                                    Utils.layer.msg('您已超过当天提现限额,每天限额提现5W');
                                    return;
                                }
                                var wit;
                                $.ajax({
                                    'type': 'POST',
                                    'url': '/withdraw/get-withdraw-info',
                                    'async': false,
                                    'dataType': 'json',
                                    'success': function(data) {
                                        wit = data.data;
                                    }
                                });

                                Utils.layer.close(index);
                                var options = '';
                                $.each(shopdata.yhk_list, function(it, i) {
                                    options += '<option value="' + it + '"   ' + ((wit.bank_code == it) ? " selected " : " ") + ' >' + i + '</option>'
                                })
                                Utils.layer.open({
                                    id: 7,
                                    type: 1,
                                    title: '提现银行卡信息',
                                    skin: 'layui-layer-rim',
                                    area: ['450px', 'auto'],
                                    content: '<div style="margin: 20px auto 0; width: 90%;">' +
                                        '<p class="boxtop" style="overflow:hidden; margin-bottom:10px;"><span class="txtitle" style=" float: left; width:25%; line-height:38px; font-size: 16px;">开户人姓名</span><input type="test" value="' + wit.bank_name + '" class="khr layui-input" style="float: right; width: 70%;" ></p>' +
                                        '<p class="boxtop" style="overflow:hidden; margin-bottom:10px;"><span class="txtitle" style=" float: left; width:25%;line-height:38px; font-size: 16px;">开户行</span>' +
                                        '<select class="khh" style="float: right; width: 70%; height: 38px; border-color:#e6e6e6;">' +
                                        '<option value="0" >请选择开户行</option>' +
                                        options +
                                        '</select>' +
                                        '</p>' +
                                        '<p class="boxtop" style="overflow:hidden; margin-bottom:10px;"><span class="txtitle" style="float:left;width:25%;line-height:38px; font-size: 16px;">转账银行卡</span><input type="test" class="zzyhk  layui-input" value="' + wit.bank_no + '" style="float: right; width: 70%;" ></p>' +
                                        '<p class="txtexts"></p>' +
                                        '<input type="hidden" class="txmoneys layui-input" value="' + txmoneys + '">' +
                                        '</div>',
                                    btn: ['下一步', '取消'],
                                    btn1: function(index, layero) {
                                        var txmoneys = $('.txmoneys').val();
                                        var zzyhk = $('.zzyhk').val();
                                        var khh = $('.khh').val();
                                        var khr = $('.khr').val();

                                        if (zzyhk == '') {
                                            Utils.layer.msg('请输入银行卡号');
                                            return;
                                        } else if (khh == '') {
                                            Utils.layer.msg('请选择开户行');
                                            return;
                                        } else if (khr == '') {
                                            Utils.layer.msg('请输入开户人姓名');
                                            return;
                                        }

                                        Utils.layer.close(index)
                                        Utils.layer.open({
                                            id: 8,
                                            type: 1,
                                            title: '管理员手机短信验证',
                                            skin: 'layui-layer-rim',
                                            area: ['280px', 'auto'],
                                            content: '<div style="margin: 20px auto 0; width: 90%;">' +
                                                '<p class="boxtop" style="line-height:1.8; color:#666; margin-bottom: 10px;"><span class="txtitle">\n' +
                                                '取现流程需要进行短信确认，验证码已发送至管理员手机：' + shopdata.admin_phone + '，请按提示操作</span></p>' +
                                                '<p class="boxtop" style="overflow:hidden;"><input type="test" class="yzms layui-input" placeholder="请输入手机验证码" style="float:left; width:55%;"  > <input type="button" value="获取验证码" class="getchecknumse layui-btn" style="float:right; padding: 0 15px;"></p>' +
                                                // onclick="getchecknumse(' + shopdata.phone + ')" 
                                                '<p class="txtexts"></p>' +
                                                '<input type="hidden" class="txmoneys layui-input" value="' + txmoneys + '">' +
                                                '<input type="hidden" class="zzyhk layui-input" value="' + zzyhk + '">' +
                                                '<input type="hidden" class="khh layui-input" value="' + khh + '">' +
                                                '<input type="hidden" class="khr layui-input" value="' + khr + '">' +
                                                '</div>',
                                            btn: ['提交提现申请', '取消'],
                                            btn1: function(index, layero) {
                                                var khr = $('.khr').val();
                                                var khh = $(".khh ").val();
                                                var zzyhk = $(".zzyhk ").val();
                                                var txmoneys = $(".txmoneys ").val();
                                                var yzm = $(".yzms ").val();
                                                if (yzm == '') {
                                                    Utils.layer.msg('请输入短信验证码');
                                                    return;
                                                }
                                                //TODO 银行卡提现
                                                $.ajax({
                                                    'type': 'POST',
                                                    'url': '/withdraw/add-wihdraw',
                                                    'data': {
                                                        withdraw_money: txmoneys,
                                                        type: 1,
                                                        zzyhk: zzyhk,
                                                        khh: khh,
                                                        khr: khr,
                                                        yzm: yzm
                                                    },
                                                    'dataType': 'json',
                                                    'success': function(data) {
                                                        if (data.code == 200) {
                                                            var shopmoney;
                                                            Utils.layer.msg('提现申请成功,待审核')
                                                            Utils.layer.close(index);
                                                            $('.yueval').html(data.data.money);
                                                            $('.order_money').html(data.data.money);
                                                            $('.service_money').html(data.data.order_money);
                                                            $('.txcount').html(data.data.count);
                                                        } else {
                                                            Utils.layer.msg(data.msg)
                                                        }
                                                    }
                                                });
                                            }
                                        })
                                        $('.getchecknumse').click(function() {
                                            getchecknumse(shopdata.phone);
                                        })

                                    }
                                });
                            },
                            btn2: function() {
                                flag2 = true;
                            }
                        });
                        $('.xzasdasdcls').keyup(function() {
                            xzasdasd($(this), service_rate, service_money)
                        })

                    } else if (isbtn == 'btnjl') {
                        Utils.layer.open({
                            title: '提现列表',
                            type: 2,
                            area: ['50%', '60%'],
                            fix: false, //不固定
                            maxmin: true,
                            content: '/withdraw/index-list',
                            success: function(layero, index) {},
                            end: function() {

                            }
                        });
                    } else if (isbtn == 'btnfw') {
                        Utils.layer.open({
                            title: '服务费明细',
                            type: 2,
                            area: ['50%', '60%'],
                            fix: false, //不固定
                            maxmin: true,
                            content: '/withdraw/service-list',
                            success: function(layero, index) {},
                            end: function() {

                            }
                        });
                    }
                } else {
                    if (!shopdata.withdraw_pwd) {
                        Utils.layer.open({
                            id: 10,
                            type: 1,
                            title: '设置提现密码',
                            skin: 'layui-layer-rim',
                            area: ['270px', 'auto'],
                            content: '<div style="margin: 20px auto 0; width:90%; ">' +
                                '<p class="boxtop" style="margin-bottom:10px;"><input type="password"  class="txmm pwds layui-input"  placeholder="请输入新的提现密码"   >' +
                                '<p class="boxtop" style="margin-bottom:15px;"><input type="password"  class="txmm reppwd layui-input"  placeholder="再次输入提现密码"   >' +
                                '<p class="boxtops" style="margin-bottom:15px; color: #999;">验证管理员手机:' + shopdata.admin_phone + '</p>' +
                                '<p class="boxtops" style="margin-bottom:10px; overflow:hidden;"><input type="test" class="yzms layui-input" style="width: 50%; float:left;" placeholder="请输入验证码"> <input type="button"  value="获取验证码" class="getchecknums layui-btn" style="float:right"></p>' +
                                // onclick="getchecknums(' + shopdata.phone + ')"
                                '<p class="txtexts"></p>' +
                                '</div>',
                            btn: ['确认', '取消'],
                            btn1: function(index, layero) {
                                var pwds = $('.pwds').val();
                                var reppwd = $('.reppwd').val();
                                var yzm = $('.yzms').val();
                                if (pwds == "") {
                                    Utils.layer.msg("请输入提现密码")
                                } else if (reppwd == "") {
                                    Utils.layer.msg("请再次输入提现密码")
                                } else if (reppwd != pwds) {
                                    Utils.layer.msg("两次输入不一致")
                                } else {
                                    $.ajax({
                                        'type': 'POST',
                                        'url': '/withdraw/update-withdraw-pwd',
                                        'data': { pwd: pwds, reppwd: reppwd, yzm: yzm },
                                        'dataType': 'json',
                                        'success': function(data) {
                                            Utils.layer.msg(data.msg)
                                            if (data.code == 200) {
                                                Utils.layer.close(index)
                                                window.location.reload()
                                            }
                                        }
                                    })
                                }

                            }
                        })
                        $('.getchecknums').click(function() {
                            getchecknums(shopdata.phone)
                        })
                        return;
                    }


                    Utils.layer.open({
                        id: 1,
                        type: 1,
                        title: '账户密码验证',
                        skin: 'layui-layer-rim',
                        area: ['270px', 'auto'],
                        content: '<div style="margin: 20px auto 0; width:90%;' +
                            '<p class="boxtop"><input type="password" class="txmm layui-input"  placeholder="请输入提现密码"   >' +
                            '<p class="txtexts"></p>' +
                            '</div>',
                        btn: ['确认', '忘记密码', '取消'],
                        btn1: function(index, layero) {
                            var pwd = $('.txmm').val();
                            $.ajax({
                                'type': 'POST',
                                'url': '/withdraw/checkpwd',
                                'data': { pwd: pwd },
                                'dataType': 'json',
                                'success': function(data) {
                                    if (data.code == 200) {
                                        $('.yueval').html(shopdata.order_money);
                                        $('.order_money').html(shopdata.money);
                                        $('.service_money').html(shopdata.service_money);
                                        $('.txcount').html(shopdata.counts);
                                        Utils.layer.close(index)
                                        $('.hidpwd').val(data.data);

                                    } else {
                                        Utils.layer.msg(data.msg)
                                    }
                                }
                            })
                        },
                        btn2: function(index, layero) {
                            Utils.layer.open({
                                id: 2,
                                type: 1,
                                title: '修改密码',
                                skin: 'layui-layer-rim',
                                area: ['270px', 'auto'],
                                content: '<div style="margin: 20px auto 0; width:90%; ">' +
                                    '<p class="boxtop" style="margin-bottom:10px;"><input type="password"  class="txmm pwds layui-input"  placeholder="请输入新的提现密码"   >' +
                                    '<p class="boxtop" style="margin-bottom:15px;"><input type="password"  class="txmm reppwd layui-input"  placeholder="再次输入提现密码"   >' +
                                    '<p class="boxtops" style="margin-bottom:15px; color: #999;">验证管理员手机:' + shopdata.admin_phone + '</p>' +
                                    '<p class="boxtops" style="margin-bottom:10px; overflow:hidden;"><input type="test" class="yzms layui-input" style="width: 50%; float:left;" placeholder="请输入验证码"> <input type="button"  value="获取验证码" class="getchecknum layui-btn" style="float:right"></p>' +
                                    // onclick="getchecknum(' + shopdata.phone + ')"
                                    '<p class="txtexts"></p>' +
                                    '</div>',
                                btn: ['确认', '取消'],
                                btn1: function(index, layero) {
                                    var pwds = $('.pwds').val();
                                    var reppwd = $('.reppwd').val();
                                    var yzm = $('.yzms').val();
                                    if (yzm == '') {
                                        Utils.layer.msg('请输入短信验证码');
                                        return;
                                    }
                                    if (pwds == '') {
                                        Utils.layer.msg('请输入新密码');
                                        return;
                                    }
                                    if (reppwd == '') {
                                        Utils.layer.msg('请再次输入新密码');
                                        return;
                                    }
                                    if (pwds != reppwd) {
                                        Utils.layer.msg('两次输入不一致');
                                        return;
                                    }
                                    $.ajax({
                                        'type': 'POST',
                                        'url': '/withdraw/update-withdraw-pwd',
                                        'data': { pwd: pwds, reppwd: reppwd, yzm: yzm },
                                        'dataType': 'json',
                                        'success': function(data) {
                                            Utils.layer.msg(data.msg)
                                            if (data.code == 200) {
                                                Utils.layer.close(index)
                                            }
                                        }
                                    })
                                }
                            })
                            $('.getchecknum').click(function() {
                                getchecknum(shopdata.phone)
                            })
                        }
                    })

                    $('.txmm').focus();
                }
            })
        })


    })
}